﻿using System.Collections;
using FlowCanvas;
using FlowCanvas.Nodes;
using NodeCanvas.Framework;
using ParadoxNotion.Design;
using UnityEngine;

namespace Yoozoo.Gameplay.Liberty.Nodes
{
    [Category("Ggame")]
    public class SwitchHasBeenRecorded : FlowControlNode
    {
        [RequiredField]
        public BBParameter<string> recordName;

        protected Coroutine co;
        public IEnumerator Invoke(string recordName,FlowOutput caseTrue,FlowOutput caseFalse,Flow f)
        {
            yield return new WaitForOneFrame();
            yield return new WaitForOneFrame();
            bool recorded = BlueprintUtils.HasBeenRecorded(recordName);
            f.Call(recorded ? caseTrue : caseFalse);
            co = null;
        }
        
        protected override void RegisterPorts()
        {
            var caseTrue = AddFlowOutput("True");
            var caseFalse = AddFlowOutput("False");
            var caseThen = AddFlowOutput("Then");
            AddFlowInput("In", (f) =>
            {
                co = StartCoroutine(Invoke(recordName.value.Trim(),caseTrue,caseFalse,f));
                f.Call(caseThen);
            });

        }

        public override void OnGraphStoped()
        {
            base.OnGraphStoped();
            if (co!=null)
            {
                StopCoroutine(co);
            }
        }
    }
}